<%@ page language="java" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
${aier_doctype}
<html <s:property value="aier_html_xmlns" escape="false"/>>
<head>
<meta http-equiv="Content-Type" content="${aier_content_type}" />
<title><s:property value="ev_title" /></title>
<meta name="keywords" content="${aier_keyworks}" />
<meta name="description" content="${aier_description}" />
<link href="${aier_css_url}jq/jq.css" rel="Stylesheet" />
<link rel="stylesheet" href="${aier_css_url}${aier_jqui_css}/ui.css" />
<link rel="stylesheet" href="${aier_css_url}social/module/jq-msg.css" />
<link rel="stylesheet" href="${aier_css_url}comm/text.css" />
<link rel="stylesheet" href="${aier_css_url}admin/admin.css" />

<script type="text/javascript" src="${aier_js_url}j.js"></script>
<script type="text/javascript" src=
"${aier_js_url}b.js"></script>
<script type="text/javascript" src="${aier_js_url}jp/msg.js"></script>
<script type="text/javascript" src="${aier_js_url}jp/1.js"></script>
<script type="text/javascript" src="${aier_js_url}lib/afu.js"></script>
<script type="text/javascript" src="${aier_js_url}jp/hh/ui.js"></script>
<script type="text/javascript" src="${aier_js_url}lib/block.js"></script>
<script type="text/javascript" src="${aier_js_url}lib/arrayList.js"></script>
<script type="text/javascript" src="${aier_js_url}jc/cj.js"></script>
<script type="text/javascript" src="${aier_js_url}jq/jq.js"></script>
<script type="text/javascript" src="${aier_js_url}jq/cn.js"></script>
<script type="text/javascript" src="${aier_js_url}comm/text.js"></script>
</head>
<body style="background-color: #EEF2FB;">
    
    <div style="background-color: #EEF2FB;width: 100%;height: 700px; ">
       
        <div id="tabs">
	        <ul>
	            <li><a href="#tabs-1">创建代理</a></li>
	        </ul>
	        <div id="tabs-1" style="height: 500px;">
	            <div class="d_content">
	                <button onclick="history.go(-1);" style="margin-left: 700px;"
                        class="b_maSel ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
                        role="button" aria-disabled="false">
                        <span class="ui-button-text">返回</span>
                    </button>
					<div class="d_scnOut" style="margin-left: 180px; margin-top: 0px;">
	                     <div class="d_scname">
	                         <label>代理商账号:</label>
	                     </div>
	                     <div class="d_sctext" >
	                         <input type="text" id="account">
	                     </div>
	                 </div>
			        <button onclick="checkAccount();"
	                     class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
	                     role="button" aria-disabled="false" style="margin-top: -76px;margin-left: 500px;">
	                     <span class="ui-button-text">确定</span>
	                 </button>
	                 <div style="margin-left: 580px;margin-top: -50px;font-size: 12px;" id = "accountText">
	                 </div>
	                 <div id="addproxy" style="display:none;">
	                     <div class="d_scnOut" style="margin-left: 60px; margin-top: 50px;">
	                         <div class="d_scname">
	                             <label>联系邮箱:</label>
	                         </div>
	                         <div class="d_sctext" >
	                             <input type="text" id="email">
	                         </div>
	                     </div>
	                     
	                     <div class="d_scnOut"  style="margin-left: 320px;margin-top: -40px;">
                             <div class="d_scname">
                                 <label>代理名称:</label>
                             </div>
                             <div class="d_sctext" >
                                 <input type="text" id="name">
                             </div>
                         </div>
	                     
	                     <div class="d_scnOut" style="margin-left: 60px;">
	                         <div class="d_scname">
	                             <label>联系电话:</label>
	                         </div>
	                         <div class="d_sctext" >
	                             <input type="text" id="phone">
	                         </div>
	                         <div style="margin-top:-15px;margin-left:300px; font-size: 12px;color: red;">如是固定电话,格式为"区号-号码"。如要加入多个号码,请用","分隔。</div>
	                     </div>
	                     
                        <div style="margin-left: 60px;height: 260px;overFlow-y: auto ;">
                            <div class="d_scnOut">
                                <div class="d_scname" style="font-size: 14px;">
                                    <label>代理区域:</label>
                                </div>
                                <div style="font-size: 14px; margin-left:120px;margin-top: -20px;" id = "pqy">
                                </div>
                                <div class="sel_region" style ="margin-top: 0px;" id = "region">
                                    <s:action name="cityutil" namespace="/common"
                                        executeResult="true">
                                        <s:param name="cityid">-1</s:param>
                                    </s:action>
                                    <button id="addpay"onclick="addPay();">添加代理区域
                                    </button>
                                </div>
                            </div>
                        </div>
                        
                        <button id="b_create"
                            class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
                            role="button" aria-disabled="false"
                            style=" margin-top: 20px; margin-left: 400px;" onclick="create();">
                            <span class="ui-button-text">新增</span>
                        </button>
                    </div>
					
				</div>
			</div>
		</div>
	</div>

	<script type="text/javascript">
	    $("#tabs").tabs();
	    var cidStr = "";
		var uid;
		//判断用户名是否存在，读取用户的电话，邮箱等信息 
		function checkAccount() {
		    uid = null;
			$("#name").val("");
			$("#email").val("");
			$("#phone").val("");
			$("#accountText").html("");
			$("#pqy").html("");
			cidStr = "";
			var fun = function(data) {
			    load(-1);
				if (data.s == 1) {
				    $("#addproxy").show();
				    $("#accountText").html("该帐号目前不是代理商用户，可创建");
					uid = data.user.uid;
					if (!isnull(data.userEmail)) {
					   $("#email").val(data.userEmail[0].email);
					}
					
					var phoneStr = "";
					for ( var i = 0; i < data.userPhone.length; i++) {
					    if (!isnull(phoneStr)) {
					       phoneStr += ",";
					    }
					    phoneStr += data.userPhone[i].phone;
					}
					$("#phone").val(phoneStr);
					
				} else if (data.s == 0) {
				    $("#addproxy").show();
					$("#accountText").html("该帐号目前不存在，可直接创建");
				} else if (data.s == 2) {
                    $("#accountText").html("该帐号已经是代理商，请重新输入");
                } 
			};
			exeAjax(
					ajaxUrl("${aier_url}${aier_urijs}/adminproxy_searchProxyByUid${aier_suffix}"),
					fun, {
						"account" : $("#account").val(),
					});
		}
		
		// 添加代理区域
        function addPay() {
            var pcid = -1;
            if(acity.cid>-1){
                pcid = acity.cid;
            }
            if (pcid == -1) {
               $().message("请选择区域!");
               return;
            }
            for (var i=0;i<cidStr.split(",").length;i++) {
                if (cidStr.split(",")[i] == pcid) {
                    $().message("已有相同区域!");
                    return;
                }
            }
            
            var address = "";
            if ($('#s__p  option:selected').val() != -1) {
                address += $('#s__p  option:selected').text();
            }
            if ($('#s__c  option:selected').val() != -1) {
                address += $('#s__c  option:selected').text();
            }
            if ($('#s__a  option:selected').val() != -1) {
                address += $('#s__a  option:selected').text();
            }
            if ($('#s__t  option:selected').val() != -1) {
                address += $('#s__t  option:selected').text();
            }
            var html = "<div id = 'address"+pcid+"'>"+"<span>"+address+"</span>"+"<Button onclick='delAddress("+pcid+")'>删除</Button></div>";
            if ($('#pqy').html() == "") {
                $('#pqy').html(html);
            } else {
                $('#pqy').html($('#pqy').html()+html);
                cidStr += ",";
            }
            cidStr += pcid;
            load(-1);
            acity.cid = -1;
        }
        // 删除代理区域
        function delAddress(cid) {
            var str = "";
            for (var i=0;i<cidStr.split(",").length;i++) {
                if (cidStr.split(",")[i] != cid) {
                    if (str != "") {
                        str+=",";
                    }
                    str += cidStr.split(",")[i];
                }
            }
            cidStr = str;
            var div = document.getElementById("address"+cid);
            if (div != null) {
                div.parentNode.removeChild(div);
            }
        }
		
		//添加代理 
		function create() {
		    var account = $('#account').val();
		    var name = $('#name').val();
		    var email = $('#email').val();
		    var phone = $('#phone').val();
			if(isnull(uid)){
				if(isnull(account.trim())){
                    $().message("请输入帐号!");
                    return;  
                }
			}
			if(isnull(name.trim())){
                $().message("请输入代理商名称!");
                return;  
            }
            if(email.trim()!=""&&check_email(email.trim())=="false"){
                $().message("邮箱"+email+"格式有误!");
                return
            }
            var phonevalue = check_phone(phone);
            if(phonevalue!="true"){
                $().message("号码"+phonevalue+"格式有误!");
                return
            }
			var fun = function(data) {
				if (data.s == 1) {
					$().message("代理添加成功");
					$("#addproxy").hide();
					$("#account").val("");
					$("#accountText").html("");
				}else {
					$().message("代理添加失败");
				}
			};
			exeAjax(
					ajaxUrl("${aier_url}${aier_urijs}/adminproxy_insertProxy${aier_suffix}"),
					fun, {"proxyName" : name,"uid" : uid, "account" : account,"email" : email,"phone" : phone,"cidStr" : cidStr});
		}
		
	</script>
</body>
</html>